package com.example.controller;

import com.example.config.RabbitMQConfig;
import com.example.model.NotificationEvent;
import com.example.model.OrderEvent;
import com.example.model.UserEvent;
import com.example.service.EventPublisher;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/events")
@RequiredArgsConstructor
@Slf4j
public class EventController {
    
    private final EventPublisher eventPublisher;
    
    @PostMapping("/user")
    public Map<String, String> publishUserEvent(@RequestBody Map<String, String> payload) {
        String userId = payload.getOrDefault("userId", UUID.randomUUID().toString());
        String username = payload.get("username");
        String action = payload.get("action");
        
        log.info("Publishing user event: userId={}, username={}, action={}", userId, username, action);
        
        UserEvent userEvent = new UserEvent(userId, username, action);
        eventPublisher.publishEvent(userEvent, RabbitMQConfig.USER_ROUTING_KEY);
        
        Map<String, String> response = new HashMap<>();
        response.put("status", "success");
        response.put("eventId", userEvent.getEventId());
        return response;
    }
    
    @PostMapping("/order")
    public Map<String, String> publishOrderEvent(@RequestBody Map<String, Object> payload) {
        String orderId = payload.getOrDefault("orderId", UUID.randomUUID().toString()).toString();
        String userId = payload.get("userId").toString();
        double amount = Double.parseDouble(payload.get("amount").toString());
        String status = payload.get("status").toString();
        
        log.info("Publishing order event: orderId={}, userId={}, amount={}, status={}", 
                 orderId, userId, amount, status);
        
        OrderEvent orderEvent = new OrderEvent(orderId, userId, amount, status);
        eventPublisher.publishEvent(orderEvent, RabbitMQConfig.ORDER_ROUTING_KEY);
        
        Map<String, String> response = new HashMap<>();
        response.put("status", "success");
        response.put("eventId", orderEvent.getEventId());
        return response;
    }
    
    @PostMapping("/notification")
    public Map<String, String> publishNotificationEvent(@RequestBody Map<String, String> payload) {
        String userId = payload.get("userId");
        String message = payload.get("message");
        String channel = payload.get("channel");
        
        log.info("Publishing notification event: userId={}, message={}, channel={}", userId, message, channel);
        
        NotificationEvent notificationEvent = new NotificationEvent(userId, message, channel);
        eventPublisher.publishEvent(notificationEvent, RabbitMQConfig.NOTIFICATION_ROUTING_KEY);
        
        Map<String, String> response = new HashMap<>();
        response.put("status", "success");
        response.put("eventId", notificationEvent.getEventId());
        return response;
    }
} 